﻿// <copyright file="Window1.xaml.cs" company="Oleg V. Polikarpotchkin">
// Copyright © 2008-2009 Oleg V. Polikarpotchkin. All Right Reserved
// </copyright>
// <author>Oleg V. Polikarpotchkin</author>
// <email>ov-p@yandex.ru</email>
// <date>2009-07-28</date>
// <summary>OpenWPFChart library. Axes Test app.</summary>
// <revision>$Id$</revision>

using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using OpenWPFChart.Helpers;
using OpenWPFChart.Parts;

namespace AxesTest
{
	/// <summary>
	/// Interaction logic for Window1.xaml
	/// </summary>
	public partial class Window1 : Window
	{
		public Window1()
		{
			InitializeComponent();
			DataContext = new ChartLinearScale(1.0, 100.0, 300.0);
		}

		static Axis FindAxis(DependencyObject parent)
		{
			Axis axis = parent as Axis;
			if (axis != null)
				return axis;
			for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
			{
				axis = FindAxis(VisualTreeHelper.GetChild(parent, i));
				if (axis != null)
					return axis;
			}
			return null;
		}

		void Properties_Click(object sender, RoutedEventArgs e)
		{
			Axis axis = FindAxis(axisHost);
			AxisPropertiesDialog dlg = new AxisPropertiesDialog(axis) { Owner = this };
			if (dlg.ShowDialog() == true)
			{
				AxisDataView axisData = dlg.AxisData;
				DataContext = axisData.AxisScale;

				Axis.SetTickLength(axisHost, axisData.TickLength);
				Axis.SetLongTickLength(axisHost, axisData.LongTickLength);
				Axis.SetLabelMargin(axisHost, axisData.LabelMargin);
				Axis.SetPen(axisHost, new Pen()
				{
					Brush = new SolidColorBrush(axisData.PenColor),
					Thickness = axisData.PenThickness
				});
				Axis.SetFontFamily(axisHost, axisData.FontFamily);
				Axis.SetFontSize(axisHost, axisData.FontSize);
				Axis.SetFontStretch(axisHost, axisData.FontStretch);
				Axis.SetFontStyle(axisHost, axisData.FontStyle);
				Axis.SetFontWeight(axisHost, axisData.FontWeight);

				UpdateLayout();
				axis = FindAxis(axisHost);
				axis.ContentLayout = axisData.ContentLayout;
				axis.LabelFormat = axisData.LabelFormat;
			}
		}

		/// <summary>
		/// Executes <see cref="NavigationCommands.Zoom"/> command.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="System.Windows.Input.ExecutedRoutedEventArgs"/> instance containing the event data.</param>
		void ZoomExecutedCommandHandler(object sender, ExecutedRoutedEventArgs e)
		{
			double zoom = 1.2;
			if (e.Parameter != null)
			{
				try
				{
					zoom = Convert.ToDouble(e.Parameter);
				}
				catch (Exception ex)
				{
					Trace.WriteLine("ZoomCommandHandler " + ex.Message);
					return;
				}
			}

			if (zoom == 0.0)
				return;
			if (zoom < 0.0)
				zoom = -1.0 / zoom;

			(DataContext as ChartScale).Scale *= zoom;
		}

		private void Exit_Click(object sender, RoutedEventArgs e)
		{
			Close();
		}
	}
}
